﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Art;

namespace System.Windows.Elements
{
    class PaletteMarker : FrameworkElement
    {
        public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register("Stroke", typeof(Pen), typeof(PaletteMarker), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure));

        public PaletteMarker()
        {
            RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
            this.SetValue(FrameworkElement.SnapsToDevicePixelsProperty, true);
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            Size desiredSize = base.MeasureOverride(availableSize);
            if (this.Stroke != null) desiredSize = new Size(Stroke.Thickness, Stroke.Thickness);
            return desiredSize;
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            Double width = this.RenderSize.Width;
            Double height = this.RenderSize.Height;

            if (this.Stroke != null)
            {
                width = Math.Max(0, width - this.Stroke.Thickness);
                height = Math.Max(0, height - this.Stroke.Thickness);
            }

            drawingContext.DrawEllipse(null, this.Stroke, new Point(width / 2, height / 2), width / 2, height / 2);
        }

        public Pen Stroke
        {
            get { return (Pen)this.GetValue(PaletteMarker.StrokeProperty); }
            set { this.SetValue(PaletteMarker.StrokeProperty, value); }
        }
    }
}
